package com.dianping.cat.configuration.web.url.transform;

import com.dianping.cat.configuration.web.url.IEntity;
import com.dianping.cat.configuration.web.url.entity.Code;
import com.dianping.cat.configuration.web.url.entity.PatternItem;
import com.dianping.cat.configuration.web.url.entity.UrlPattern;
import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;




public class DefaultJsonParser<T>
{
  private DefaultLinker m_linker;
  private Stack<String> m_tags;
  private Stack<Object> m_objs;
  private List<T> m_entities;
  private Class<?> m_entityClass;

  private DefaultJsonParser(Class<?> entityClass) {
    this.m_linker = new DefaultLinker(true);

    this.m_tags = new Stack();

/*  33 */     this.m_objs = new Stack();

/*  35 */     this.m_entities = new ArrayList();




/*  40 */     this.m_entityClass = entityClass;
  }


/*  44 */   public static <T extends IEntity<T>> T parse(Class<T> entityClass, InputStream in) throws IOException { return (T)parse(entityClass, new InputStreamReader(in, "utf-8")); }



  public static <T extends IEntity<T>> T parse(Class<T> entityClass, Reader reader) throws IOException {
/*  49 */     DefaultJsonParser parser = new DefaultJsonParser(entityClass);

/*  51 */     parser.onArrayBegin();
/*  52 */     parser.parse(new JsonReader(reader));
/*  53 */     parser.onArrayEnd();

/*  55 */     if (parser.m_entities.isEmpty()) {
/*  56 */       return null;
    }
/*  58 */     return (T)(IEntity)parser.m_entities.get(0);
  }



/*  63 */   public static <T extends IEntity<T>> T parse(Class<T> entityClass, String json) throws IOException { return (T)parse(entityClass, new StringReader(json)); }



/*  67 */   public static <T extends IEntity<T>> List<T> parseArray(Class<T> entityClass, InputStream in) throws Exception { return parseArray(entityClass, new InputStreamReader(in, "utf-8")); }



  public static <T extends IEntity<T>> List<T> parseArray(Class<T> entityClass, Reader reader) throws Exception {
/*  72 */     DefaultJsonParser parser = new DefaultJsonParser(entityClass);

/*  74 */     parser.parse(new JsonReader(reader));
/*  75 */     return (List<T>) parser.m_entities;
  }


/*  79 */   public static <T extends IEntity<T>> List<T> parseArray(Class<T> entityClass, String json) throws Exception { return parseArray(entityClass, new StringReader(json)); }




  protected <T> T convert(Class<T> type, String value, T defaultValue) {
/*  85 */     if (value == null || value.length() == 0) {
/*  86 */       return defaultValue;
    }

/*  89 */     if (type == Boolean.class)
/*  90 */       return (T)Boolean.valueOf(value); 
/*  91 */     if (type == Integer.class)
/*  92 */       return (T)Integer.valueOf(value); 
/*  93 */     if (type == Long.class)
/*  94 */       return (T)Long.valueOf(value); 
/*  95 */     if (type == Short.class)
/*  96 */       return (T)Short.valueOf(value); 
/*  97 */     if (type == Float.class)
/*  98 */       return (T)Float.valueOf(value); 
/*  99 */     if (type == Double.class)
/* 100 */       return (T)Double.valueOf(value); 
/* 101 */     if (type == Byte.class)
/* 102 */       return (T)Byte.valueOf(value); 
/* 103 */     if (type == Character.class) {
/* 104 */       return (T)Character.valueOf(value.charAt(0));
    }
/* 106 */     return (T)value;
  }


  private Object createRootEntity() {
    try {
/* 112 */       return this.m_entityClass.newInstance();

    }
/* 115 */     catch (Exception e) {
/* 116 */       throw new RuntimeException(String.format("Unable to create entity(%s) instance!", new Object[] { this.m_entityClass.getName() }), e);
    }
  }


/* 121 */   private boolean isTopLevel() { return (this.m_objs.size() == 1); }


  protected void onArrayBegin() {
/* 125 */     if (this.m_objs.isEmpty()) {
/* 126 */       this.m_objs.push(this.m_entities);
/* 127 */       this.m_tags.push("");
    }
  }
  protected void onArrayEnd() {
/* 131 */     this.m_objs.pop();
/* 132 */     this.m_tags.pop();
  }


/* 136 */   protected void onName(String name) { this.m_tags.push(name); }


  protected void onObjectBegin() {
/* 140 */     if (isTopLevel()) {
/* 141 */       this.m_objs.push(createRootEntity());
/* 142 */       this.m_tags.push("");
    } else {
/* 144 */       Object parent = this.m_objs.peek();
/* 145 */       String tag = (String)this.m_tags.peek();

/* 147 */       if (parent instanceof UrlPattern) {
/* 148 */         if ("pattern-items".equals(tag) || "codes".equals(tag)) {
/* 149 */           this.m_objs.push(parent);
        } else {
/* 151 */           String parentTag = (this.m_tags.size() >= 2) ? (String)this.m_tags.get(this.m_tags.size() - 2) : null;

/* 153 */           if ("pattern-items".equals(parentTag)) {
/* 154 */             PatternItem patternItems = new PatternItem();

/* 156 */             this.m_linker.onPatternItem((UrlPattern)parent, patternItems);
/* 157 */             this.m_objs.push(patternItems);
/* 158 */           } else if ("codes".equals(parentTag)) {
/* 159 */             Code codes = new Code();

/* 161 */             this.m_linker.onCode((UrlPattern)parent, codes);
/* 162 */             this.m_objs.push(codes);
          } else {
/* 164 */             throw new RuntimeException(String.format("Unknown tag(%s) found at %s!", new Object[] { tag, this.m_tags }));
          }
        }
      } else {
/* 168 */         throw new RuntimeException(String.format("Unknown tag(%s) found at %s!", new Object[] { tag, this.m_tags }));
      }
    }
  }

  protected void onObjectEnd() {
/* 174 */     this.m_tags.pop();

/* 176 */     Object entity = this.m_objs.pop();

/* 178 */     if (isTopLevel()) {
/* 179 */       this.m_entities.add((T) entity);
    }
  }

  protected void onValue(String value) {
/* 184 */     Object parent = this.m_objs.peek();
/* 185 */     String tag = (String)this.m_tags.pop();

/* 187 */     if (parent instanceof UrlPattern) {
/* 188 */       parseForUrlPattern((UrlPattern)parent, tag, value);
/* 189 */     } else if (parent instanceof PatternItem) {
/* 190 */       parseForPatternItem((PatternItem)parent, tag, value);
/* 191 */     } else if (parent instanceof Code) {
/* 192 */       parseForCode((Code)parent, tag, value);
    } else {
/* 194 */       throw new RuntimeException(String.format("Unknown tag(%s) under %s!", new Object[] { tag, parent }));
    }
  }

  private void parse(JsonReader reader) throws IOException {
    try {
/* 200 */       reader.parse(this);
/* 201 */     } catch (EOFException e) {
/* 202 */       if (this.m_objs.size() > 1) {
/* 203 */         throw new EOFException(String.format("Unexpected end while parsing json! tags: %s.", new Object[] { this.m_tags }));
      }
    }

/* 207 */     this.m_linker.finish();
  }

  public void parseForCode(Code code, String tag, String value) {
/* 211 */     if ("id".equals(tag)) {
/* 212 */       code.setId((Integer)convert(Integer.class, value, null));
/* 213 */     } else if ("name".equals(tag)) {
/* 214 */       code.setName(value);
/* 215 */     } else if ("status".equals(tag)) {
/* 216 */       code.setStatus((Integer)convert(Integer.class, value, null));
    } else {
/* 218 */       throw new RuntimeException(String.format("Unknown tag(%s) of %s under %s!", new Object[] { tag, code, this.m_tags }));
    }
  }

  public void parseForPatternItem(PatternItem patternItem, String tag, String value) {
/* 223 */     if ("group".equals(tag)) {
/* 224 */       patternItem.setGroup(value);
/* 225 */     } else if ("name".equals(tag)) {
/* 226 */       patternItem.setName(value);
/* 227 */     } else if ("pattern".equals(tag)) {
/* 228 */       patternItem.setPattern(value);
/* 229 */     } else if ("domain".equals(tag)) {
/* 230 */       patternItem.setDomain(value);
/* 231 */     } else if ("id".equals(tag)) {
/* 232 */       patternItem.setId(((Integer)convert(Integer.class, value, Integer.valueOf(0))).intValue());
    } else {
/* 234 */       throw new RuntimeException(String.format("Unknown tag(%s) of %s under %s!", new Object[] { tag, patternItem, this.m_tags }));
    }
  }

  public void parseForUrlPattern(UrlPattern urlPattern, String tag, String value) {
/* 239 */     if ("pattern-items".equals(tag) || "codes".equals(tag)) {
      return;
    }
/* 242 */     throw new RuntimeException(String.format("Unknown tag(%s) of %s under %s!", new Object[] { tag, urlPattern, this.m_tags }));
  }

  static class JsonReader {
    private Reader m_reader;
    private char[] m_buffer;

    public JsonReader(Reader reader) {
/* 250 */       this.m_buffer = new char[2048];






/* 257 */       this.m_reader = reader;
    }
    private int m_size; private int m_index;
    private char next() throws IOException {
/* 261 */       if (this.m_index >= this.m_size) {
/* 262 */         this.m_size = this.m_reader.read(this.m_buffer);
/* 263 */         this.m_index = 0;

/* 265 */         if (this.m_size == -1) {
/* 266 */           throw new EOFException();
        }
      }

/* 270 */       return this.m_buffer[this.m_index++];
    }

    public void parse(DefaultJsonParser parser) throws IOException {
/* 274 */       StringBuilder sb = new StringBuilder();
/* 275 */       boolean flag = false;

      while (true) {
/* 278 */         char ch = next();

/* 280 */         switch (ch) {
          case '\t':
          case '\n':
          case '\r':
          case ' ':
            continue;
          case '{':
/* 287 */             parser.onObjectBegin();
/* 288 */             flag = false;
            continue;
          case '}':
/* 291 */             if (flag) {
/* 292 */               parser.onValue(sb.toString());
/* 293 */               sb.setLength(0);
            }

/* 296 */             parser.onObjectEnd();
/* 297 */             flag = false;
            continue;
          case '"':
          case '\'':
            while (true) {
/* 302 */               char ch2 = next();

/* 304 */               if (ch2 != ch) {
/* 305 */                 if (ch2 == '\\') {
/* 306 */                   char ch3 = next();

/* 308 */                   switch (ch3) {
                    case 't':
/* 310 */                       sb.append('\t');
                      continue;
                    case 'r':
/* 313 */                       sb.append('\r');
                      continue;
                    case 'n':
/* 316 */                       sb.append('\n');
                      continue;
                  }
/* 319 */                   sb.append(ch3);

                  continue;
                }
/* 323 */                 sb.append(ch2); continue;
              }  break;
            }
/* 326 */             if (!flag) {
/* 327 */               parser.onName(sb.toString());
            } else {
/* 329 */               parser.onValue(sb.toString());
/* 330 */               flag = false;
            }

/* 333 */             sb.setLength(0);
            continue;




          case ':':
/* 340 */             if (sb.length() != 0) {
/* 341 */               parser.onName(sb.toString());
/* 342 */               sb.setLength(0);
            }

/* 345 */             flag = true;
            continue;
          case ',':
/* 348 */             if (sb.length() != 0) {
/* 349 */               if (!flag) {
/* 350 */                 parser.onName(sb.toString());
              } else {
/* 352 */                 parser.onValue(sb.toString());
              }

/* 355 */               sb.setLength(0);
            }

/* 358 */             flag = false;
            continue;
          case '[':
/* 361 */             parser.onArrayBegin();
/* 362 */             flag = false;
            continue;
          case ']':
/* 365 */             parser.onArrayEnd();
/* 366 */             flag = false;
            continue;
        }
/* 369 */         sb.append(ch);
      }
    }
  }
}


/* Location:              /Users/hanfei/Documents/apache-tomcat-9.0.24/webapps/cat/WEB-INF/lib/cat-core-3.0.0.jar!/com/dianping/cat/configuration/web/url/transform/DefaultJsonParser.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       1.0.7
 */